import { mapServer } from "../server";

class Polygon {
    /**
     * 更新线要素
     * @param {Object} options
     * @param {Array} options.coordinates 区域坐标点数组，[[x1,y1],[x2,y2],...]
     * @param {Array} options.attr 区域要素属性配置数组，[{name:"name",value:"value",type:"string"}]
     * @param {Object} options.service 地图服务配置对象
     * @param {Object} options.docLayer 地图文档图层对象
     */
    static add({
                   coordinates,
                   attr,
                   service,
                   docLayer
               }) {

        //创建mapgis要素数据 geometry + style + attribute
        var pointObj = [];
        coordinates.forEach(function (item) {
            var point = new Zondy.Object.Point2D(item[0], item[1]);
            pointObj.push(point);
        });


        //设置区要素的几何信息
        var gArc = new Zondy.Object.Arc(pointObj);
        //构成区要素折线
        var gAnyLine = new Zondy.Object.AnyLine([gArc]);
        //构成区要素
        var gRegion = new Zondy.Object.GRegion([gAnyLine]);
        //构成区要素的几何信息
        var fGeom = new Zondy.Object.FeatureGeometry({RegGeom: [gRegion]});
        //随机输出1~1502之间的整数
        var fillColor = 2;

        //设置区要素的图形参数信息
        var cRegionInfo = new Zondy.Object.CRegionInfo({
            EndColor: 1,
            FillColor: fillColor,
            FillMode: 0,
            OutPenWidth: 1,
            OverMethod: 0,
            PatAngle: 1,
            PatColor: 1,
            PatHeight: 1,
            PatID: 27,
            PatWidth: 1
        });
        //要素图形参数信息
        var graphicInfo = new Zondy.Object.WebGraphicsInfo({InfoType: 3, RegInfo: cRegionInfo});

        //设置区要素的属性信息
        var attValue = [];

        //创建一个新的区要素
        var newFeature = new Zondy.Object.Feature({AttValue: attValue, fGeom: fGeom, GraphicInfo: graphicInfo});
        newFeature.setFType(3);

        //创建一个要素数据集
        var featureSet = new Zondy.Object.FeatureSet();
        var fldNumber = attr.length;
        var fldType = attr.map(item => item.type);
        var fldName = attr.map(item => item.name);
        var cAttValue = new Zondy.Object.CAttStruct({FldNumber: fldNumber, FldType: fldType, FldName: fldName});
        featureSet.AttStruct = cAttValue;
        featureSet.addFeature(newFeature);

        console.log(featureSet, service)
        //创建一个要素编辑服务对象
        var editDocFeature = new Zondy.Service.EditDocFeature(service.docName, service.layerId, mapServer);
        editDocFeature.add(featureSet, this.onPloySuccess(docLayer));
    }




    /**
     * 修改线要素
     * @param {Object} options
     * @param {Array} options.coordinates 区域坐标点数组，[[x1,y1],[x2,y2],...]
     * @param {Array} options.attr 区域要素属性配置数组，[{name:"name",value:"value",type:"string"}]
     * @param {Object} options.service 地图服务配置对象
     * @param {Object} options.docLayer 地图文档图层对象
     * @param {Array} options.featureIds 要修改的要素的ID数组
     */
    static update({
                      coordinates,
                      attr,
                      service,
                      docLayer,
                      featureIds
                  }) {
        //获取当前选择的要素编号

        //创建mapgis要素数据 geometry + style + attribute
        var pointObj = [];
        coordinates.forEach(function (item) {
            var point = new Zondy.Object.Point2D(item[0], item[1]);
            pointObj.push(point);
        });

        //设置区要素的几何信息
        var gArc = new Zondy.Object.Arc(pointObj);
        //构成区要素折线
        var gAnyLine = new Zondy.Object.AnyLine([gArc]);
        //构成区要素
        var gRegion = new Zondy.Object.GRegion([gAnyLine]);
        //构成区要素的几何信息
        var featureObj = new Zondy.Object.FeatureGeometry({RegGeom: [gRegion]});



        //随机输出1~1502之间的整数作为符号填充颜色
        var symColor = 2;
        //设置区要素的图形参数信息
        var cRegionInfo = new Zondy.Object.CRegionInfo({
            EndColor: 1,
            FillColor: symColor,
            FillMode: 0,
            OutPenWidth: 1,
            OverMethod: 0,
            PatAngle: 1,
            PatColor: 1,
            PatHeight: 1,
            PatID: 27,
            PatWidth: 1
        });
        //要素图形参数信息
        var graphicInfo = new Zondy.Object.WebGraphicsInfo({
            InfoType: 3,
            RegInfo: cRegionInfo
        });

        //设置区要素的属性信息
        var attValue = [];

        //创建一个新的区要素
        var newFeature = new Zondy.Object.Feature({
            AttValue: attValue,
            fGeom: featureObj,
            GraphicInfo: graphicInfo
        });
        newFeature.setFType(3);
        newFeature.setFID(featureIds);//要修改的要素的ID

        //创建一个要素数据集
        var featureSet = new Zondy.Object.FeatureSet();
        var fldNumber = attr.length;
        var fldType = attr.map(item => item.type);
        var fldName = attr.map(item => item.name);
        var cAttValue = new Zondy.Object.CAttStruct(
            {
                FldNumber: fldNumber,
                FldType: fldType,
                FldName: fldName
            }
        );
        featureSet.AttStruct = cAttValue;
        featureSet.addFeature(newFeature);

        //创建一个要素编辑服务对象
        var editDocFeature = new Zondy.Service.EditDocFeature(service.docName, service.layerId, mapServer);
        editDocFeature.update(featureSet, this.onPloySuccess(docLayer));

    }

    static onPloySuccess(docLayer) {
        return function (rlt) {
            var result = rlt;
            if (result) {
                alert("操作面要素成功！");
                docLayer.refresh();
            } else {
                alert("操作面要素失败！");
            }
        }
    }
}